<?php
@ob_start();
@session_start();

include("../connect/connect.php");
include("../function/thaidate.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>-</title>
<link rel="stylesheet" href="stylereport.css" type="text/css" />
<link rel="shortcut icon" href="../image/Letter-T.ico" />
</head>
<?php
//AR
$Query1 = mysql_query("SELECT sale_report2.cus_name as cus_name, product.prod_name as prod_name, sale_report2.sale_price as sale_price, sale_report2.sale_qty as sale_qty, sale_report2.sale_discount as sale_discount FROM sale_report2,product WHERE sale_report2.prod_id = product.prod_id AND sale_report2.stype_id = '3'");

	$dQuery = mysql_query("SELECT min(sale_datetime) as sale_datetime FROM sale_report2");
	$dnQuery = mysql_fetch_array($dQuery);
	
	$lQuery = mysql_query("SELECT * FROM table_status");
	$lNum = mysql_num_rows($lQuery);
?>
<body <?php if($lNum=='0' && $dnQuery['sale_datetime']!=""){ echo "onload=\"window.print()\""; } ?>>
<font size="5"><strong>รายงานการขายแยกตามประเภทการขาย</strong></font><br><br>
<font size="4"><strong>
<?php 
	if($lNum>'0'){
		echo "<font color=\"red\">กรุณาเช็คบิลให้ครบทุกโต๊ะก่อนค่ะ</font>";
	}else if($dnQuery['sale_datetime']!=""){
		echo "ประจำ".ThaiDate($dnQuery['sale_datetime']);

?>
</strong></font><br>
<font size="3"><strong>พนักงานขาย : <?php echo $_SESSION["user_name"]." ".$_SESSION["user_surname"]; ?></strong></font>
<h2>AR</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td width="20%">ชื่อ</td>
<td>รายการ</td>
<td>หน่วยละ</td>
<td>จำนวน</td>
<td>ราคาขาย</td>
<td>ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num1 = 0;
while($row1 = mysql_fetch_array($Query1)) {
	$num1++;
?>
<tr>
<td align="center"><?php echo $num1; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row1['cus_name']; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row1['prod_name']; ?></td>
<td align="right" style="padding-right:15px"><?php echo "".number_format($row1['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php echo $row1['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo number_format($row1['sale_price']*$row1['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount1 = ($row1['sale_price']*$row1['sale_qty'])+$row1['sale_discount'];
echo "".number_format($sale_discount1, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal1=$strTotal1+$sale_discount1;
}
?>
<tfoot>
  <tr>
    <td colspan="6" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal1, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>

<?php
//oc
$Query2 = mysql_query("SELECT sale_report2.cus_name as cus_name, product.prod_name as prod_name, sale_report2.sale_price as sale_price, sale_report2.sale_qty as sale_qty FROM sale_report2,product WHERE sale_report2.prod_id = product.prod_id AND sale_report2.stype_id = '4' ");
?>
<h2>OC</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td width="20%">ชื่อ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="10%">จำนวน</td>
<td width="15%">รวมเงิน</td>
</tr>
  </thead>
<?php
$num2 = 0;
while($row2 = mysql_fetch_array($Query2)) {
	$num2++;
?>
<tr>
<td align="center"><?php echo $num2; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row2['cus_name']; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row2['prod_name']; ?></td>
<td align="right" style="padding-right:15px"><?php echo "".number_format($row2['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php echo $row2['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount2 = $row2['sale_price']*$row2['sale_qty']; 
echo "".number_format($sale_discount2, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal2=$strTotal2+$sale_discount2;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal2, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>

<?php
//Entertain
$Query3 = mysql_query("SELECT sale_report2.cus_name as cus_name, product.prod_name as prod_name, sale_report2.sale_price as sale_price, sale_report2.sale_qty as sale_qty FROM sale_report2,product WHERE sale_report2.prod_id = product.prod_id AND sale_report2.stype_id = '5'");
?>
<h2>Entertain</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td width="20%">ชื่อ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="10%">จำนวน</td>
<td width="15%">รวมเงิน</td>
</tr>
  </thead>
<?php
$num3 = 0;
while($row3 = mysql_fetch_array($Query3)) {
	$num3++;
?>
<tr>
<td align="center"><?php echo $num3; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row3['cus_name']; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row3['prod_name']; ?></td>
<td align="right" style="padding-right:15px"><?php echo "".number_format($row3['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php echo $row3['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount3 = $row3['sale_price']*$row3['sale_qty']; 
echo "".number_format($sale_discount3, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal3=$strTotal3+$sale_discount3;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal3, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>


<?php
//Comp
$Query8 = mysql_query("SELECT sale_report2.cus_name as cus_name, product.prod_name as prod_name, sale_report2.sale_price as sale_price, sale_report2.sale_qty as sale_qty FROM sale_report2,product WHERE sale_report2.prod_id = product.prod_id AND sale_report2.stype_id = '7'");
?>
<h2>Comp</h2>
<table class="listorder">
  <thead>
<tr>
<td width="5%">ลำดับ</td>
<td width="20%">ชื่อ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="10%">จำนวน</td>
<td width="15%">รวมเงิน</td>
</tr>
  </thead>
<?php
$num8 = 0;
while($row8 = mysql_fetch_array($Query8)) {
	$num8++;
?>
<tr>
<td align="center"><?php echo $num8; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row8['cus_name']; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row8['prod_name']; ?></td>
<td align="right" style="padding-right:15px"><?php echo "".number_format($row8['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php echo $row8['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount8 = $row8['sale_price']*$row8['sale_qty']; 
echo "".number_format($sale_discount8, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal8=$strTotal8+$sale_discount8;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal8, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>

<?php
//สด
$Query4 = mysql_query("SELECT product.prod_name as prod_name, sale_report2.sale_price as sale_price, sum(sale_report2.sale_qty) as sale_qty, sum(sale_report2.sale_discount) as sale_discount FROM sale_report2,product WHERE sale_report2.prod_id = product.prod_id AND sale_report2.stype_id = '1' GROUP BY sale_report2.prod_id ORDER BY sum(sale_report2.sale_qty) DESC");
?>
<h2>สด</h2>
<table class="listorder">
  <thead>
<tr>
<td width="1%">ลำดับ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="10%">จำนวน</td>
<td width="15%">ราคาขาย</td>
<td width="15%">ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num4 = 0;
while($row4 = mysql_fetch_array($Query4)) {
	$num4++;
?>
<tr>
<td align="center"><?php echo $num4; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row4['prod_name']; ?></td>
<td align="right" style="padding-right:15px"><?php echo "".number_format($row4['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php echo $row4['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo number_format($row4['sale_price']*$row4['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount4 = ($row4['sale_price']*$row4['sale_qty'])+$row4['sale_discount']; 
echo "".number_format($sale_discount4, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal4=$strTotal4+$sale_discount4;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal4, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>


<!-- บัตรเครดิต -->
<?php
$Query7 = mysql_query("SELECT product.prod_name as prod_name, sale_report2.sale_price as sale_price, sum(sale_report2.sale_qty) as sale_qty, sale_report2.sale_discount as sale_discount FROM sale_report2,product WHERE sale_report2.prod_id = product.prod_id AND sale_report2.stype_id = '6' GROUP BY sale_report2.prod_id,sale_report2.sale_price,sale_report2.sale_discount ORDER BY sum(sale_report2.sale_qty) DESC");
?>
<h2>บัตรเครดิต</h2>
<table class="listorder">
  <thead>
<tr>
<td width="1%">ลำดับ</td>
<td>รายการ</td>
<td width="12%">หน่วยละ</td>
<td width="10%">จำนวน</td>
<td width="15%">ราคาขาย</td>
<td width="15%">ราคา + Sv.</td>
</tr>
  </thead>
<?php
$num7 = 0;
while($row7 = mysql_fetch_array($Query7)) {
	$num7++;
?>
<tr>
<td align="center"><?php echo $num7; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row7['prod_name']; ?></td>
<td align="right" style="padding-right:15px"><?php echo "".number_format($row7['sale_price'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php echo $row7['sale_qty']; ?></td>
<td align="right" style="padding-right:10px"><?php echo number_format($row7['sale_price']*$row7['sale_qty'], 2,'.',','); ?></td>
<td align="right" style="padding-right:10px"><?php $sale_discount7 = ($row7['sale_price']*$row7['sale_qty'])+$row7['sale_discount']; 
echo "".number_format($sale_discount7, 2,'.',',');
?></td>
</tr>
<?php
	$strTotal7=$strTotal7+$sale_discount7;
}
?>
<tfoot>
  <tr>
    <td colspan="5" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal7, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>


<?php
//รับชำระหนี้
$Query6 = mysql_query("SELECT * FROM debtor_report_session");
?>
<h2>รับชำระหนี้</h2>
<table class="listorder">
  <thead>
<tr>
<td>ลำดับ</td>
<td>ชื่อ</td>
<td>วันที่ค้างชำระ</td>
<td>เลขที่บิล</td>
<td>ยอดเงิน</td>
</tr>
  </thead>
<?php
$num6 = 0;
while($row6 = mysql_fetch_array($Query6)) {
	$num6++;
?>
<tr>
<td align="center"><?php echo $num6; ?></td>
<td align="left" style="padding-left:10px"><?php echo $row6['debtor_report_name']; ?></td>
<td align="left" style="padding-left:10px"><?php echo thai_date(strtotime($row6['debtor_report_date'])); ?></td>
<td align="center"><?php echo $row6['debtor_report_bill']; ?></td>
<td align="right" style="padding-right:10px"><?php echo "".number_format($row6['debtor_report_total'], 2,'.',','); ?></td>
</tr>
<?php
	$strTotal6=$strTotal6+$row6['debtor_report_total'];
}
?>
<tfoot>
  <tr>
    <td colspan="4" align="center">รวมเงิน</td>
    <td align="right" style="padding-right:10px"><?php echo "".number_format($strTotal6, 2,'.',','); ?></td>
	</tr>
</tfoot>
</table>

<div class="endtotal">รวมทั้งหมด <?php echo "".number_format($strTotal1+$strTotal2+$strTotal3+$strTotal4+$strTotal5+$strTotal6+$strTotal7+$strTotal8+$strTotal9, 2,'.',',')." บาท"; ?></div>
<div style="padding-top: 20px">
<input type="button" name="endsale" id="endsale" value="ปิดการขาย" onclick="javascript:if(confirm('ยืนยันปิดการขาย')==true){window.location='report_sale2.php?action=endsale';}" ></div>
<?php } else {
echo "<font color=\"red\">ยังไม่มีรายการขายของวันนี้ค่ะ</font>";
}
?>
</body>

</html>
